import {showOrder,findByOrder,buy} from "../service/order"

export default {
    namespaced: true,
    state: {
        orders: [],
        order:{},
        orderId:"",
        dialogVisible: false,
        searchParams: { type: "", value: "" },
        pagination: {}
    },
    mutations: {
        setOrders(state, orders) {
            state.orders = orders;
        },
        setOrder(state,order){
            state.order = order;
        },
        setOrderId(state,orderId){
            state.orderId = orderId;
        },
        setSearchParams(state, searchParams) {
            state.searchParams = searchParams;
        },
        setDialogVisible(state, dialogVisible) {
            state.dialogVisible = dialogVisible
        },
        setPagination(state, pagination) {
            state.pagination = pagination;
        }
    },
    actions: {
        async lookOrder(context, orderId) {
            let response = await findByOrder(orderId);
            context.commit("setOrder", response)
        },
        async showOrder(context, pageParam={}) {
            if (pageParam && pageParam.type) {
                context.commit("setSearchParams", pageParam);
            } else {
                if(pageParam){
                    pageParam.type = context.state.searchParams.type;
                    pageParam.value = context.state.searchParams.value;
                }
            }
            let response = await showOrder(pageParam);
            context.commit("setPagination", response);
            context.commit("setOrders", response.list);
        },
        // async addOrder(context, user) {
        //      await buy(user);
        //     // context.commit("setOrder", response)
        // },
    }
}